package com.zhaosc.mybatis.shared.db;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class DataSourceInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		Method method = invocation.getMethod();
		DataSourceSwitch dataSourceSwitch = method.getAnnotation(DataSourceSwitch.class);
		DataSourceEnum dataSourceEnum = dataSourceSwitch.dataSource();
		DynamicDataSourceHolder.setDbType(dataSourceEnum.value);
		Object obj = invocation.proceed();
		DynamicDataSourceHolder.clearDbType();
		return obj;
	}

}
